package com.ykht.proj.coupon.timer;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ykht.proj.coupon.Enum.CmsTypeEnum;
import com.ykht.proj.coupon.Enum.WeightTypeEnum;
import com.ykht.proj.coupon.entity.FxCmsInfo;
import com.ykht.proj.coupon.entity.FxWeight;
import com.ykht.proj.coupon.entity.FxWeightInfo;
import com.ykht.proj.coupon.mapper.FxCmsInfoMapper;
import com.ykht.proj.coupon.service.FxCmsInfoService;
import com.ykht.proj.coupon.vo.WeightInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lazyBoy
 * @Date: 2021/2/20 10:19
 */
@Component
@SuppressWarnings("all")
@Slf4j
@EnableScheduling
public class CmsInfoTimer {
	@Resource
	private FxCmsInfoMapper fxCmsInfoMapper;
	@Resource
	private FxCmsInfoService fxCmsInfoService;

	/**
	 * 定时发布文章
	 * 十分钟
	 */
	@Scheduled(cron = "0 */10 * * * ?")
	public void  couponLogExpireTimer(){
		log.info("->定时任务：文章自动发布");
		/*获取当前全部的定时发布的草稿
		* 判断定时发布时间是否等于小于当前时间
		* 是就自动发布
		* */
		List<FxCmsInfo> list = fxCmsInfoMapper.selectAll();
		for (FxCmsInfo fxCmsInfo : list) {
			if(fxCmsInfo.getPublishTime()!=null){
				if(LocalDateTime.now().isAfter(fxCmsInfo.getPublishTime())){
					fxCmsInfo.setUpdateTime(LocalDateTime.now());
					fxCmsInfo.setStatus(CmsTypeEnum.CMS_TYPE_3.getValue());
					fxCmsInfoMapper.updateById(fxCmsInfo);
				}
			}
		}
	}
//	@Scheduled(cron = "0 */2 * * * ?")
	@Scheduled(cron = "0 */10 * * * ?")
	public void  hots(){
		log.info("->定时任务：文章计算热度值");
		List<FxCmsInfo> list = fxCmsInfoMapper.selectList(Wrappers.lambdaQuery(FxCmsInfo.class).eq(FxCmsInfo::getStatus,CmsTypeEnum.CMS_TYPE_3.getValue()));

		List<FxWeight> weights = new FxWeight().selectList(Wrappers.lambdaQuery(FxWeight.class).eq(FxWeight::getStatus, 0));

		List<String> types = weights.stream().map(FxWeight::getWeightType).collect(Collectors.toList());

		if (types.size()>0){
			List<WeightInfo> fxWeightInfos = new FxWeightInfo().selectList(Wrappers.lambdaQuery(FxWeightInfo.class).in(FxWeightInfo::getWeightType, types))
					.stream().map(o->{
						WeightInfo weightInfo = new WeightInfo();
						weightInfo.setWeightInfo(o);
						weights.stream().filter(oo->oo.getWeightType().equals(o.getWeightType())).findAny().ifPresent(oo->weightInfo.setWeight(oo));
						return weightInfo;
					}).collect(Collectors.toList());

			Map<String, List<WeightInfo>> map = fxWeightInfos.stream().collect(Collectors.groupingBy(oo->oo.getWeight().getWeightType()));

			list.stream().forEach(o->{
				JSONObject jo = JSONUtil.parseObj(o);
				BigDecimal hotsTemp = BigDecimal.ZERO;


				for (Map.Entry<String, List<WeightInfo>> entry : map.entrySet()) {
					String k = entry.getKey();
					List<WeightInfo> v = entry.getValue();
					System.out.println(JSONUtil.toJsonStr(map));
					switch (WeightTypeEnum.getThisByVal(k)){
						default:
							WeightTypeEnum e = WeightTypeEnum.getThisByVal(k);
							for (WeightInfo oo : v) {
								if (oo.getWeight().getWeightType().equals(k) && oo.getWeightInfo().getLevelLeft()!=null && oo.getWeightInfo().getLevelLeft() <= jo.getInt(e.getField())
										&& ((oo.getWeightInfo().getLevelRight()!=null && oo.getWeightInfo().getLevelRight() >= jo.getInt(e.getField())) || oo.getWeightInfo().getLevelRight()==null)){
									hotsTemp = hotsTemp.add(oo.getWeightInfo().getLevelValue().multiply(oo.getWeight().getWeightValue()));
								}
							}
							break;
					}
				}
				o.setHots(hotsTemp);
			});
			System.out.println(JSONUtil.toJsonStr(list));
			fxCmsInfoService.saveOrUpdateBatch(list,100);
		}
	}

//	public static void main(String[] args) {
//		BigDecimal a = BigDecimal.ZERO;
//		System.out.println(a.add(BigDecimal.valueOf(2)));
//		System.out.println(a);
//	}

}
